/**
 * Provider implementation that stores preferences using a RESTful Resource.
 */
Ext.define("AmpleHarvest.addOns.state.cRESTProvider",
		{
			m_str_URL			: "user/preferances.php",
			m_str_ID			: "test123",
			extend				: 'Ext.state.Provider',
			constructor			: function(config)
			{
				this.callParent();
				Ext.apply(this, config);
				
				this.get_State(this);

			},
			set_str_ID			:function(str_Value)
			{
				this.m_str_ID = str_Value;
			},
			
			set					: function(str_Name, str_Value)
			{
				if (typeof str_Value == "undefined" || str_Value === null)
				{
					this.clear(str_Name);
					return;
				}
				if (!this.state)
				{
					this.state = new Object();
				}
				this.state[str_Name] = str_Value;
				this.setPreference(str_Name, str_Value);
				this.fireEvent("statechange", this, str_Name, str_Value);
			},
			get					: function(str_StateID, obj_Default)
			{
				// console.log("get - " + str_StateID);
				// console.log(obj_Default);
				//
				// console.log(this);
				// console.trace();
				Ext.Ajax.request(
						{
							url		: this.m_str_URL,
							method	: "GET",
							params	:
							{
								id			: this.m_str_ID,
								component	: str_StateID
							},
							success	: function(obj_Response)
							{
								obj_Json = Ext
										.decode(obj_Response.responseText);

								if (this.m_arr_Objects)
								{
									if (this.m_arr_Objects[str_StateID])
									{
										var obj_Me = this.m_arr_Objects[str_StateID];
										if (obj_Json.data)
										{
											obj_Json.data = Ext.apply({},
													obj_Json.data
											);
											if (obj_Me.fireEvent(
													'beforestaterestore',
													obj_Me, obj_Json.data
											) !== false)
											{
												obj_Me
														.applyState(obj_Json.data);
												obj_Me.fireEvent(
														'staterestore', obj_Me,
														obj_Json.data
												);
											}
										}
									}
								}
							},
							failure	: function(obj_Response)
							{
								this.remove_StateObject(str_StateID);
								alert("Failed to get State");
							},
							scope	: this
						}
				);
				return {};// this.state;
			},
			clear				: function(name)
			{
				//console.log("clear");
				return;
				this.clearPreference(name);
				Ext.state.RESTfulProvider.superclass.clear.call(this, name);
			},
			get_State			: function(obj_Provider)
			{
				this.state = null;
				Ext.Ajax.request(
						{
							url		: this.m_str_URL,
							method	: "GET",
							params	:
							{
								id	: obj_Provider.m_str_ID
							},
							success	: function(obj_Response)
							{
								// console.log(this);
								// console.log(this.state);
								obj_Json = Ext
										.decode(obj_Response.responseText);
								if (obj_Json.data == null)
								{
									obj_Json.data = {};
								}
								this.state = obj_Json.data;
								// console.log(this.state);
								// process server response here
							},
							failure	: function(obj_Response)
							{
								this.state = {}
								// process server response here
							},
							scope	: this
						});
				return {};

			},
			setPreference		: function(str_Name, obj_Value)
			{
				Ext.Ajax.request(
						{
							url		: this.m_str_URL,
							method	: "POST",
							params	:
							{
								id		: this.m_str_ID,
								key		: str_Name,
								value	: Ext.encode(obj_Value)
							},
							success	: function(obj_Response)
							{
							},
							failure	: function(obj_Response)
							{
								alert("Failed to set preferance");

								// console.log(obj_Response.responseText);
								// process server response here
							}
						});

			},
			clearPreference		: function(name)
			{
				console.log("clearPref");
				return;
				Ext.Ajax.request(
				{		// makes an async request
							url		: this.url + "/" + escape(name),
							method	: 'DELETE',
							success	: function(response, options)
							{ // call back for the async request
								console
										.log('value asynchronously deleted in database'
												+ response.responseText);
							},
							failure	: function(response, options)
							{
								// console.log('defaulting to cookies.');
							}
						}
				);
			},
			add_StateObject		: function(str_StateID, obj_Object)
			{
				if (!this.m_arr_Objects)
				{
					this.m_arr_Objects = [];
				}
				this.m_arr_Objects[str_StateID] = obj_Object;
			},
			remove_StateObject	: function(str_StateID)
			{
				if (!this.m_arr_Objects)
				{
					return;
				}
				delete this.m_arr_Objects[str_StateID];
			}
		}
);